﻿/****************************** Module Header ******************************\
* Module Name:  SearchFilterEngine.cs
* Project:	    YR.CUIT.Framework
* Copyright (c) Yogaratnam Rahulan.
* 
* The class SearchFilterEngine is a search Helper of Object which runs within Internet
* Explorer and offers additional services.
* 
* A SearchFilterEngine is a dynamic-link library (DLL) capable of searching and filtering itself to any new 
* instance of Internet Explorer or Windows Explorer. Such a module can get in touch 
* with the browser through the container's at run time. 
* 
* This source is subject to the Yogaratnam Rahulan Public License.
* See Soon
* All other rights reserved.
* 
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/



using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UITesting;

namespace YR.CUIT.SearchFilterEngine
{
    public static class SearchFilterEngine
    {
        public static T SearchAndFilterObjects<T>(this UITestControl tCrl, dynamic sProp, dynamic fProp = null)
            where T : UITestControl, new()
        {
            var ctrl = new T { Container = tCrl };
            IEnumerable<string> pNames = ((object)sProp).GetPropObject();

            foreach (var item in pNames)
            {
                ctrl.SearchProperties.Add(item, ((object)sProp).GetPropValue(item).ToString());
            }

            if (fProp != null)
            {
                pNames = ((object)fProp).GetPropObject();
                foreach (var item in pNames)
                {
                    ctrl.SearchProperties.Add(item, ((object)fProp).GetPropValue(item).ToString());
                }
            }

            return ctrl;
        }

        private static IEnumerable<string> GetPropObject(this object obj)
        {
            return (from x in obj.GetType().GetProperties() select x.Name).ToList();
        }

        private static object GetPropValue(this object obj, string pName)
        {
            var prop = (from x in obj.GetType().GetProperties() where x.Name == pName select x).FirstOrDefault();
            return prop.GetValue(obj, new object[] { });
        }

    }
}
